<template>
  <div>
    三级联动
    <el-form :inline="true"
             :model="formInline"
             class="demo-form-inline">
      <el-form-item label="一级分类">
        <el-select v-model="formInline.category1Id"
                   placeholder="请选择"
                   @change="getCateGory1Id"
                   :disabled="isShowTable">
          <el-option :label="c1.name"
                     :value="c1.id"
                     v-for="(c1, index) in list1"
                     :key="c1.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="formInline.category2Id"
                   placeholder="请选择"
                   @change="getCateGory2Id"
                   :disabled="isShowTable">
          <el-option :label="c2.name"
                     :value="c2.id"
                     v-for="(c2, index) in list2"
                     :key="c2.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="formInline.category3Id"
                   placeholder="请选择"
                   :disabled="isShowTable"
                   @change="getCateGory3Id">
          <el-option :label="c3.name"
                     :value="c3.id"
                     v-for="(c3, index) in list3"
                     :key="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelect",
  props: ['isShowTable'],
  components: {},
  data () {
    return {
      formInline: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
      list1: [],
      list2: [],
      list3: [],
    };
  },
  mounted () {
    this.getList();
  },
  methods: {
    // 一级
    async getList () {
      const result = await this.$API.attr.reqCateGory1Id();
      if (result.code === 200) {
        this.list1 = result.data;
      }
    },
    // 二级
    async getCateGory1Id (value) {
      this.formInline.category2Id = "";
      this.formInline.category3Id = "";
      this.list2 = [];
      this.list3 = [];
      const CategorySelect = { categoryId: value, id: 1 }
      this.$emit('getCateList', CategorySelect)
      const result = await this.$API.attr.reqCateGory2Id(value);
      if (result.code === 200) {
        this.list2 = result.data;
      }
    },
    // 三级
    async getCateGory2Id (value) {
      this.formInline.category3Id = "";
      this.list3 = [];
      const CategorySelect = { categoryId: value, id: 2 }
      this.$emit('getCateList', CategorySelect)
      const result = await this.$API.attr.reqCateGory3Id(value);
      if (result.code === 200) {
        this.list3 = result.data;
      }
    },
    getCateGory3Id (value) {
      const CategorySelect = { categoryId: value, id: 3 }
      this.$emit('getCateList', CategorySelect)
    },
  },
};
</script>

<style scoped lang="scss"></style>
